package com.hgx.web.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

public class JaxWsDynamicClientFactoryTest {

    public static void main(String[] args) {
        Client client = initClient("http://localhost:8080/spring_cxf_war/HelloWorld?wsdl");
        try {
            Object[] result = client.invoke("sayHello", "哈哈哈哈", 18);
            String outParam = "";
            System.out.println(result.length);
            if (result.length > 0) {
                Object object = result[0];
                if (object instanceof byte[]) {
                    outParam = new String((byte[]) object);
                } else {
                    outParam = (String) object;
                }

            }
            System.out.println(outParam);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Client initClient(String url) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(url);
        HTTPConduit http = (HTTPConduit) client.getConduit();
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        httpClientPolicy.setConnectionTimeout(30000);
        httpClientPolicy.setAllowChunking(false);
        httpClientPolicy.setReceiveTimeout(60000);
        http.setClient(httpClientPolicy);
        return client;
    }
}
